//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Verifications
{
    using System;
    using System.Linq;
    using Model;

    public static class VerificationExtensions
    {
        public static bool MustSatisfy(params Func<bool>[] conditions)
        {
            return conditions.Aggregate(true, (current, condition) => current && condition());
        }

        public static bool SuccessfullyYieldsNonNullValue<TSetting>(this Func<TSetting> func, string name, VerificationResult result) where TSetting : class
        {
            return func.SuccessfullyYieldsValue(name, result) && func.DoesNotYieldNullValue(name, result);
        }

        public static bool SuccessfullyYieldsNonNullOrEmptyValue(this Func<string> func, string name, VerificationResult result)
        {
            return func.SuccessfullyYieldsValue(name, result) && func.DoesNotYieldNullValueOrEmpty(name, result);
        }

        public static bool SuccessfullyYieldsValue<TSetting>(this Func<TSetting> func, string name, VerificationResult result)
        {
            try
            {
                func();
                return true;
            }
            catch (Exception exception)
            {
                result.AddMessage(MessageType.Error, "Could not get {0} value", name);
                result.AddException(exception);
                return false;
            }
        }

        public static bool DoesNotYieldNullValue<TSetting>(this Func<TSetting> func, string name, VerificationResult result) where TSetting : class
        {
            if (func() == null)
            {
                result.AddMessage(MessageType.Error, "{0} is null", name);
                return false;
            }

            return true;
        }

        public static bool DoesNotYieldNullValueOrEmpty(this Func<string> func, string name, VerificationResult result)
        {
            if (string.IsNullOrWhiteSpace(func()))
            {
                result.AddMessage(MessageType.Error, "{0} is null or empty", name);
                return false;
            }

            return true;
        }
    }
}
